<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    /*重新设置样式*/
    .form-control {
        width: 196px !important;
        height: 34px !important;
        padding: 10px !important;
    }

</style>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>物资在库金额统计</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px; margin-bottom: 5px">
                                    <label>环比年份选择</label>
                                    <select style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width: 65px!important;"
                                            class="form-control" name="year"></select>
                                </div>
                                <div class="form-group" style="margin-right: 10px; margin-bottom: 5px">
                                    <label>环比月份选择</label>
                                    <select style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width: 40px!important;"
                                            class="form-control" name="month"></select>
                                </div>

                                <div class="form-group" style="margin-right: 10px; margin-bottom: 5px">
                                    <button type="button" class="btn btn-success" id="btn-search">
                                        <span class="Bold">搜索</span>
                                    </button>
                                </div>

                            </div>
                        </div>
                    </form>
                </div>
                <div id="toolbar" class="form-group">

                </div>
                <div class="box-body">
                    <table id="bootstrap-table" style="table-layout: fixed;"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /**
     * 加载年月
     * */
    function loadYearAndMonth() {
        let beginYear = 2020;
        let endYear = new Date().getFullYear();
        let monthTextArray = ["", "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
        $("#queryForm [name='year']").empty();
        for (let i = beginYear; i <= endYear; i++) {
            if(i === endYear){
                $("#queryForm [name='year']").append("<option selected='selected' value='" + i + "'>" + i + "</option>");
            }else{
                $("#queryForm [name='year']").append("<option value='" + i + "'>" + i + "</option>");
            }
        }
        let currentMonth = new Date().getMonth();
        $("#queryForm [name='month']").empty();
        for (let i = 1; i < monthTextArray.length; i++) {
            if (i - 1 === currentMonth - 1) {
                $("#queryForm [name='month']").append("<option selected='selected' value='" + i + "'>" + monthTextArray[i] + "</option>");
            } else {
                $("#queryForm [name='month']").append("<option value='" + i + "'>" + monthTextArray[i] + "</option>");
            }
        }
    }
    /**
     * 获取手工帐环比结果
     * */
    function getResult() {
        if($("#beforeManualAccount").val()!=null &&
            $("#beforeManualAccount").val()!=undefined &&
            $("#beforeManualAccount").val()!='' &&
            $("#afterManualAccount").val()!=null &&
            $("#afterManualAccount").val()!=undefined &&
            $("#afterManualAccount").val()!=''
        ){
            $("#result").text( eval((($("#afterManualAccount").val() - $("#beforeManualAccount").val()) / $("#beforeManualAccount").val()) * 100).toFixed(2)+'%');
        }
    }

    /**
     * 加载数据
     */
    function loadData() {
        $.ajax(
            {
                url: ctx + 'overview/statistical/getMaterialInStockData',
                type: 'post',
                traditional: true,// 阻止深度序列化， 使参数可以使用数组
                dataType: 'json',
                contentType: "application/x-www-form-urlencoded",
                data: $("#queryForm").serialize(),
                async: true,                // 异步执行
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        $("#bootstrap-table").empty();
                        let month = $("#queryForm [name='month']").val();
                        let year = $("#queryForm [name='year']").val();
                        let beforeYear = year;
                        let beforeMonth = eval(month-1);
                        if(parseInt(month)===1) {
                            beforeYear=eval(year - 1);
                            beforeMonth = 12;
                        }
                        let headHtml = '<thead><tr><th>序号</th><th>库存类型</th><th>' + beforeYear+'年'+beforeMonth + '月统计（元）</th><th>'+year+'年'+ month + '月统计（元）</th><th>环比增长</th></tr></thead>';
                        $("#bootstrap-table").append(headHtml);
                        let bodyHtml = '<tbody id="bootstrap-table-tbody"></tbody>';
                        $("#bootstrap-table").append(bodyHtml);
                        let rowNum=1;
                        for (let i = 0; i < result.data.beforeList.length; i++) {
                            let title = '';
                            let trHtml = '';
                            if (result.data.beforeList[i].timType === '暂缓和完成-月统计' && result.data.afterList[i].timType === '暂缓和完成-月统计') {
                                title = '暂缓和完成';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '暂缓和完成-电气-月统计' && result.data.afterList[i].timType === '暂缓和完成-电气-月统计') {
                                title = '电气件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '暂缓和完成-液压-月统计' && result.data.afterList[i].timType === '暂缓和完成-液压-月统计') {
                                title = '液压件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '暂缓和完成-机械-月统计' && result.data.afterList[i].timType === '暂缓和完成-机械-月统计') {
                                title = '机械件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '项目在建-月统计' && result.data.afterList[i].timType === '项目在建-月统计') {
                                title = '项目在建';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '项目在建-电气-月统计' && result.data.afterList[i].timType === '项目在建-电气-月统计') {
                                title = '电气件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '项目在建-液压-月统计' && result.data.afterList[i].timType === '项目在建-液压-月统计') {
                                title = '液压件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '项目在建-机械-月统计' && result.data.afterList[i].timType === '项目在建-机械-月统计') {
                                title = '机械件';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            } else if (result.data.beforeList[i].timType === '车间计划-月统计' && result.data.afterList[i].timType === '车间计划-月统计') {
                                title = '车间计划';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '物资在库金额-月统计' && result.data.afterList[i].timType === '物资在库金额-月统计') {
                                title = '物资在库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '营口库存金额-月统计' && result.data.afterList[i].timType === '营口库存金额-月统计') {
                                title = '营口机械备件库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '营口库存金额-月统计-库存在库' && result.data.afterList[i].timType === '营口库存金额-月统计-库存在库') {
                                title = '营口械备件库库存在库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '营口库存金额-月统计-项目在库' && result.data.afterList[i].timType === '营口库存金额-月统计-项目在库') {
                                title = '营口械备件库项目在库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计' && result.data.afterList[i].timType === '机械备件库库存金额-月统计') {
                                title = '辽阳机械备件库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计-库存在库' && result.data.afterList[i].timType === '机械备件库库存金额-月统计-库存在库') {
                                title = '辽阳机械备件库库存在库金额';trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计-项目在库' && result.data.afterList[i].timType === '机械备件库库存金额-月统计-项目在库') {
                                title = '辽阳机械备件库项目在库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }else if (result.data.beforeList[i].timType === '总在库金额-月统计' && result.data.afterList[i].timType === '总在库金额-月统计') {
                                title = '总在库金额';
                                trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                            }

                            rowNum++;
                            $("#bootstrap-table-tbody").append(trHtml);
                        }
                    } else {
                        js.modal.warning(result.msg);

                        // if(result.msg === '无此月份数据，故无法进行环比,是否直接获取当前数据进行环比,如果进行获取会造成整个系统的卡顿，是否继续？'){
                        //     js.modal.confirm(result.msg,function () {
                        //         $.ajax(
                        //             {
                        //                 url: ctx + 'overview/statistical/getMaterialInStockDataByNowData',
                        //                 type: 'post',
                        //                 traditional: true,// 阻止深度序列化， 使参数可以使用数组
                        //                 dataType: 'json',
                        //                 contentType: "application/x-www-form-urlencoded",
                        //                 data: $("#queryForm").serialize(),
                        //                 async: true,                // 异步执行
                        //                 success: function (result) {
                        //                     if (result.type === web_status.SUCCESS) {
                        //                         js.modal.success(result.msg);
                        //                         $("#bootstrap-table").empty();
                        //                         let month = $("#queryForm [name='month']").val();
                        //                         let headHtml = '<thead><tr><th>序号</th><th>类型</th><th>' + eval(month - 1) + '月统计（吨）</th><th>' + month + '月统计（吨）</th><th>环比增长</th></tr></thead>';
                        //                         $("#bootstrap-table").append(headHtml);
                        //                         let bodyHtml = '<tbody id="bootstrap-table-tbody"></tbody>';
                        //                         $("#bootstrap-table").append(bodyHtml);
                        //                         let rowNum=1;
                        //                         for (let i = 0; i < result.data.beforeList.length; i++) {
                        //                             let title = '';
                        // if (result.data.beforeList[i].timType === '暂缓和完成-月统计') {
                        //     title = '暂缓和完成';
                        // } else if (result.data.beforeList[i].timType === '暂缓和完成-电气-月统计') {
                        //     title = '电气件';
                        // } else if (result.data.beforeList[i].timType === '暂缓和完成-液压-月统计') {
                        //     title = '液压件';
                        // } else if (result.data.beforeList[i].timType === '暂缓和完成-机械-月统计') {
                        //     title = '机械件';
                        // } else if (result.data.beforeList[i].timType === '项目在建-月统计') {
                        //     title = '项目在建';
                        // } else if (result.data.beforeList[i].timType === '项目在建-电气-月统计') {
                        //     title = '电气件';
                        // } else if (result.data.beforeList[i].timType === '项目在建-液压-月统计') {
                        //     title = '液压件';
                        // } else if (result.data.beforeList[i].timType === '项目在建-机械-月统计') {
                        //     title = '机械件';
                        // } else if (result.data.beforeList[i].timType === '车间计划-月统计') {
                        //     title = '车间计划';
                        // }else if (result.data.beforeList[i].timType === '物资在库金额-月统计') {
                        //     title = '物资在库金额';
                        // }else if (result.data.beforeList[i].timType === '营口库存金额-月统计') {
                        //     title = '营口机械备件库金额';
                        // }else if (result.data.beforeList[i].timType === '营口库存金额-月统计-库存在库') {
                        //     title = '营口械备件库库存在库金额';
                        // }else if (result.data.beforeList[i].timType === '营口库存金额-月统计-项目在库') {
                        //     title = '营口械备件库项目在库金额';
                        // }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计') {
                        //     title = '辽阳机械备件库金额';
                        // }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计-库存在库') {
                        //     title = '辽阳机械备件库库存在库金额';
                        // }else if (result.data.beforeList[i].timType === '机械备件库库存金额-月统计-项目在库') {
                        //     title = '辽阳机械备件库项目在库金额';
                        // }else if (result.data.beforeList[i].timType === '总在库金额-月统计') {
                        //     title = '总在库金额';
                        // }
                        //                             let trHtml = '<tr><th>' + rowNum + '</th><th>' + title + '</th><th>' + result.data.beforeList[i].timData + '</th><th>' + result.data.afterList[i].timData + '</th><th>' + eval(((result.data.afterList[i].timData - result.data.beforeList[i].timData) / result.data.beforeList[i].timData) * 100).toFixed(2) + '%</th></tr>';
                        //                             rowNum++;
                        //                             $("#bootstrap-table-tbody").append(trHtml);
                        //                         }
                        //                     }else {
                        //                         // js.modal.warning(result.msg);
                        //                     }
                        //                 },
                        //                 error: function () {
                        //                     js.modal.fail();
                        //                 }
                        //             }
                        //         );
                        //     });
                        // }else {
                        //      js.modal.warning(result.msg);
                        // }
                    }
                },
                error: function () {
                    js.modal.fail();
                }
            }
        );
    }

    $(function () {
        js.table.init({
            showExport: false});

        /**
         * 加载年月
         */
        loadYearAndMonth();
        /**
         * 加载数据
         */
        loadData();
        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            loadData();
        });
    });
</script>
</body>
</html>